package profile.utils

/**
  * Created by hunter.coder 涛哥  
  * 2019/4/24 15:05
  * 交流qq:657270652
  * Version: 1.0
  * 更多学习资料：https://blog.csdn.net/coderblack/
  * Description:
  **/
object TagCombineUtil {

  /**
    * 合并两个map，相同key的value累加
    * @param tagMap1
    * @param tagMap2
    * @return
    */
  def mergeMap(m1: Map[String, Double], m2: Map[String, Double]): Map[String, Double] = {
    (m1 /: m2) {
      case (map, (k, v)) =>
      map + (k -> (v + map.getOrElse(k, 0d)))
    }
  }

  def mergeTagMap(tagMap1: Map[String, Map[String, Double]], tagMap2: Map[String, Map[String, Double]]): Map[String, Map[String, Double]] = {

    (tagMap1 /: tagMap2) {
      case (map, (k, v)) => {
        if (map.get(k).isDefined) {
          val m1 = map.get(k).get
          map + (k -> mergeMap(m1, v))
        } else {
          map + (k -> v)
        }

      }
    }
  }


  /**
    * 测试
    * @param args
    */
  def main(args: Array[String]): Unit = {
    val idsTag1 = Map("imei" -> Map("imei012" -> 5.0), "idfa" -> Map("idfa02" -> 4.0), "androidid" -> Map("androidid02" -> 4.0, "androidid01" -> 1.0))
    val idsTag2 = Map("imei" -> Map("imei012" -> 3.0,"imei011"->2.0), "phone" -> Map("13667568" -> 2.0))

    println(mergeTagMap(idsTag1, idsTag2))

  }
}
